<?php
include_once '../../lib/config.php';
include_once '../../lib/db.php';
include '../../lib/utils/date.php';
include '../../lib/utils/tag.php';
include '../../model/nilai.php';
include '../../model/rewarddanpunishment.php';
include '../../model/detil_prestasipelanggaran.php';
include '../../model/prestasipelanggaran.php';
include '../../model/rangking.php';

$proc = $_REQUEST['proc'];
	
switch ($proc){
	case 'nilai-threshold':
	$detachievID = $_REQUEST['detachievID'];
	include '../../view/client/dashboard/rewardWarning_nilai_detil.php';
	break;
	
	case md5("rangking-data-check"):
		/**
		 * Syncronize data rangking saat terjadi perbuhana periode/bulan. 
		 * 1. Proses sync dengan menambahkan record baru ke table RANKING, berdasarkan record satu periode sebelumnya.
		 * 2. Penambahan record bedasarkan karyawan yang masih aktif 
		 */
		
		//current date
		$date = time();
		$cm = date("m",$date);
		$cy = date("Y",$date);
		
		//last month
		$lm = $cm-1;
		$ly = $cy;
		if ($lm<=0){
			$lm = 12;
			$ly = $cy-1;
		}
		
		$ex=true;
		
		/*
		 * 1. Select karyawan yang tidak berstatus NON-AKTIF.
		 * 2. Buat record untuk periode berikutnya    
		 */
		$sql = rank_select(false, "year(a.RANK_PERIODE)='$ly' AND month(a.RANK_PERIODE)='$lm' AND b.ID_STATUS_KARYAWAN!='STS-03'");
		while ($rr = mysql_fetch_assoc($sql)){
			
			//tidak ditemukan, tambahkan record
			if ( !rank_exist($rr['KODE_KARYAWAN'], $cm, $cy) ){
				$ex &= rank_insert('', $rr['KODE_KARYAWAN'], 0, "$cy-$cm-01", $rr['RANK_POINT']);
				echo "$cm-$cy-01";
			}
			
		}
		echo json_encode(array('error'=> !$ex, 'msg'=> mysql_innodb_error(mysql_errno())));	 return;
}